/*
  tv_functions.cpp - Library for SFDB scoreboard.
  Created by Guido Zuliani (zulio), January 14, 2012.
  Released into the public domain.
*/
#include "tv_functions.h"
#include "pitches.h"
#include "sfdblogo.h"
#include "slam.h"
#include "numbers.h"


void tv_presentation(TVout& TV)
{
   //Presentation   
  TV.select_font(font6x8);
  TV.set_cursor(45,40);
  TV.println("Zulio\n");
  TV.delay(2000);
  TV.set_cursor(35,50);
  TV.println("presents\n");
  TV.delay(1500);
  TV.clear_screen();
  //TV.bitmap(20,30,SFDBLogo);
  TV.bitmap(20,30,BigNumbers);
  TV.delay(2000);
  TV.select_font(font8x8);
  TV.set_cursor(23,60);
  TV.println("SCOREBOARD\n");
  tv_play_melody(TV);
  TV.delay(1000);
  TV.clear_screen(); 
}

void tv_sfdb(TVout& TV)
{
  TV.clear_screen();
  TV.bitmap(20,30,SFDBLogo);
  tv_play_melody(TV);
  TV.clear_screen();
  TV.bitmap(0,41,SlamLogo);
  //refresh_screen();
}

void tv_play_melody(TVout& TV)
{

  const int base_millis = 6000;  

  int melody[] = {NOTE_C3,NOTE_D3,NOTE_E3,NOTE_F3,
                  NOTE_C3,NOTE_D3,NOTE_E3,NOTE_F3, 
                  NOTE_C3,NOTE_D3,NOTE_E3,NOTE_FS3,
                  NOTE_C3,NOTE_D3,NOTE_E3,NOTE_FS3,
                  NOTE_G2, NOTE_A2,NOTE_B2, NOTE_DS3};                
  int noteDurations[] = {8,9,10,11,
                         8,9,10,11,
                         8,9,10,11,
                         8,9,10,11,
                         8,9,10,11
                       };

  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) 
  {
    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = base_millis / 4, eighth note = base_millis/8, etc.
    int noteDuration = base_millis/noteDurations[thisNote];
    //tone(11, melody[thisNote],noteDuration);
    TV.tone(melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    //noTone(11);
    TV.noTone();
  }   
}


void tv_horn(TVout& TV)
{
  TV.tone(NOTE_C4);
  delay(3500);
  TV.noTone();
}

void tv_scoreboard_background(TVout& TV)
{
  TV.clear_screen();
  //Logo
  TV.bitmap(0,41,SlamLogo);
  //TV.bitmap(85,45,PlayLogo);
  TV.select_font(font4x6);
  TV.print(0, 90, "by SFDB"); 
}

void tv_scoreboard_refresh(TVout& TV, ScoreBoard& SB)
{ 

  unsigned int h = TV.hres();
  unsigned int v = TV.vres();

  //Points
  const int point_x_pos = 46;
  const int point_y_pos = 4;
  const int score_pos = 15;
  TV.select_font(font8x8ext);
  TV.print(point_y_pos, 0, "HOME     GUEST");
  TV.select_font(font8x8ext);
  int pos = score_pos;
  if(SB.home_score()>=10)
    pos = pos-4;
  TV.print(pos-4, point_y_pos+6, "  ");
  TV.print(pos, point_y_pos+6, SB.home_score()); 
  pos = h-score_pos-12;
  if(SB.guest_score()>=10)
    pos = pos-4;
  TV.print(pos-4, point_y_pos+6, "  ");
  TV.print(pos, point_y_pos+6, SB.guest_score());
  
  //Arrow
  const int arrow_x_pos = 50;
  const int arrow_y_pos = 5;
  const int arrow_lenght = 15;
  const int arrow_pointer = 5;
  TV.draw_line(arrow_x_pos,arrow_y_pos,arrow_x_pos+arrow_lenght,arrow_y_pos,1);
  
  unsigned int left_color=1;
  unsigned int right_color=0;
  if(SB.arrow_left())
  {
    left_color=0;
    right_color=1;
  }
  TV.draw_line(arrow_x_pos,arrow_y_pos,arrow_x_pos+arrow_pointer,arrow_y_pos-arrow_pointer,left_color);
  TV.draw_line(arrow_x_pos,arrow_y_pos,arrow_x_pos+arrow_pointer,arrow_y_pos+arrow_pointer,left_color);
  TV.draw_line(arrow_x_pos+arrow_lenght,arrow_y_pos,arrow_x_pos+arrow_lenght-arrow_pointer,arrow_y_pos-arrow_pointer,right_color);
  TV.draw_line(arrow_x_pos+arrow_lenght,arrow_y_pos,arrow_x_pos+arrow_lenght-arrow_pointer,arrow_y_pos+arrow_pointer,right_color); 
  
  //Bouns
  const int bonus_x_pos = 46;
  const int bonus_y_pos = arrow_y_pos + 20;
  const int circle_pos = 35;
  const int circle_radius = 3;
  TV.select_font(font6x8);
  TV.print(bonus_x_pos, bonus_y_pos, "bonus");
  unsigned int fillcolor;
  int bonus = SB.bonus();
  if(bonus==0 || bonus==2)
    fillcolor = 0;
  else
    fillcolor = 1;
  TV.draw_circle(circle_pos,bonus_y_pos+circle_radius,circle_radius,1,fillcolor);
  if(bonus==0 || bonus==1)
    fillcolor = 0;
  else
    fillcolor = 1;
  TV.draw_circle(h-circle_pos,bonus_y_pos+circle_radius,circle_radius,1,fillcolor);
  
  //Time
  const int time_x_pos = 46;
  const int time_y_pos = bonus_y_pos+20;
  TV.select_font(font6x8);
  
  TV.set_cursor(time_x_pos, time_y_pos);
  TV.print("Time");
  //TV.print(time_x_pos, time_y_pos, "Time");
  TV.select_font(font4x6);
  if(SB.time_running())
    TV.print("          ");
  else
    TV.print(" (stopped)");
    
  TV.select_font(font8x8ext);
  TV.set_cursor(time_x_pos-6,time_y_pos+9);
  int minutes = SB.minutes();
  int seconds = SB.seconds();
  int decimals = SB.decimals();
  if(minutes<10)
    TV.print("0");
  TV.print(minutes);
  TV.print(":");
  if(seconds<10)
    TV.print("0");
  TV.print(seconds);
  if(decimals!=DO_NOT_SHOW)
  {
    TV.print(".");
    TV.print(decimals);
  }
  else
    TV.print("  ");
  
  //Period
  const int period_x_pos = 40;
  const int period_y_pos = time_y_pos+30;
  TV.select_font(font6x8);
  TV.print(period_x_pos, period_y_pos, "Period");
  TV.select_font(font8x8);
  TV.print(period_x_pos+13, period_y_pos+8, SB.period());
    
  //Debug
  //!!!!!!!
  TV.select_font(font4x6);
  TV.set_cursor(80,65);
  //TV.print(SB.last_millis()/1000); 
}

